<template>
  <div class="container">
    <el-form :model="formData" ref="listForm" label-width="120px">
      <el-form-item
        v-for="(item, index) in formItmeList"
        :key="item.code"
        :label="item.name"
        :prop="item.code"
        :rules="[
          {
            required: item.is_required,
            message: '请完善' + item.name,
            trigger: type_list.includes(item.item_type) ? 'change' : 'blur',
          },
        ]"
      >
        <el-input
          v-if="item.item_type == 'input'"
          v-model="formData[item.code]"
          :placeholder="item.pl"
          :disabled="item.disabled"
          show-word-limit
          :maxlength="item.max_length"
        >
        </el-input>
        <el-input-number
          v-if="item.item_type == 'input_number'"
          :precision="item.precision"
          :max="item.max"
          :min="item.min"
          v-model="formData[item.code]"
          :placeholder="item.pl"
          :disabled="item.disabled"
          show-word-limit
          :maxlength="item.max_length"
        >
        </el-input-number>
        <el-input
          type="textarea"
          style="width: 100%"
          v-if="item.item_type == 'textarea'"
          resize="none"
          :autosize="{ minRows: 2, maxRows: 4 }"
          v-model="formData[item.code]"
          :placeholder="item.pl"
          :disabled="item.disabled"
          show-word-limit
          :maxlength="item.max_length"
        >
        </el-input>
        <el-select
          clearable
          v-if="item.item_type == 'select' && !item.multiple"
          v-model="formData[item.code]"
          :placeholder="item.pl"
          :disabled="item.disabled"
          @change="EventBus.emit('change_selector')"
        >
          <el-option
            v-for="item2 in item.data"
            :value="item2.value"
            :label="item2.label"
            :disabled="item2.disabled"
          ></el-option>
        </el-select>
        <el-select
          clearable
          v-if="item.item_type == 'select' && item.multiple"
          multiple
          v-model="formData[item.code]"
          :placeholder="item.pl"
          :disabled="item.disabled"
          @change="EventBus.emit('change_selector')"
        >
          <el-option
            v-for="item2 in item.data"
            :value="item2.value"
            :label="item2.label"
            :disabled="item2.disabled"
          ></el-option>
        </el-select>
        <el-radio-group
          v-if="item.item_type == 'radio'"
          v-model="formData[item.code]"
          :disabled="item.disabled"
          @change="EventBus.emit('change_selector')"
        >
          <el-radio
            v-for="item2 in item.data"
            :label="item2.value"
            :disabled="item2.disabled"
            >{{ item2.label }}</el-radio
          >
        </el-radio-group>
        <el-checkbox-group
          v-if="item.item_type == 'checkBox'"
          v-model="formData[item.code]"
          :disabled="item.disabled"
          @change="EventBus.emit('change_selector')"
        >
          <el-checkbox
            v-for="item2 in item.data"
            :label="item2.value"
            :disabled="item2.disabled"
            >{{ item2.label }}</el-checkbox
          >
        </el-checkbox-group>
        <div class="flex" v-if="item.item_type == 'time'">
          <el-time-select
            :disabled="item.disabled"
            v-model="formData[item.code]"
            :max-time="item.endTime"
            style="width: 150px"
            :placeholder="item.is_time_between ? '开始时间' : '请选择'"
            start="00:00"
            :step="item.step"
            end="24:00"
          />
          <span v-if="item.is_time_between" class="m_0_5">-</span>
          <el-time-select
            :disabled="item.disabled"
            v-if="item.is_time_between"
            v-model="formData[item.code]"
            :min-time="item.startTime"
            style="width: 150px"
            placeholder="结束时间"
            start="00:00"
            :step="item.step"
            end="24:00"
          />
        </div>
        <el-date-picker
          v-if="item.item_type == 'date'"
          v-model="formData[item.code]"
          :type="item.date_type"
          unlink-panels
          range-separator="-"
          placeholder="请选择"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :disabled="item.disabled"
          value-format="x"
        />
        <el-cascader
          clearable
          v-if="item.item_type == 'tree'"
          v-model="formData[item.code]"
          :options="item.data"
          :props="item.props"
          :disabled="item.disabled"
          @change="EventBus.emit('change_selector')"
        />
        <el-switch
          v-if="item.item_type == 'switch'"
          v-model="formData[item.code]"
          active-text="开启"
          inactive-text="关闭"
          inline-prompt
          :disabled="item.disabled"
          @change="EventBus.emit('change_selector')"
        />
        <upload
          v-if="item.item_type == 'upload'"
          :imageUrl="item.imageUrl"
          :width="item.width"
          :height="item.height"
          :limit="item.limit"
          :uploadUrl="item.uploadUrl"
          :downloadUrl="item.downloadUrl"
          :accept="item.accept"
          @change="EventBus.emit('change_selector')"
        ></upload>
      </el-form-item>
      <div class="tr m_top10">
        <el-button @click="$emit('close')">取消</el-button>
        <el-button type="primary" @click="submit(listForm)">确定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { toRefs, ref } from "vue";
import upload from "@/view/components/upload.vue";
import EventBus from "@/utils/EventBus";
import { FormInstance, ElMessage, ElRadio } from "element-plus";

import axios from "@/service/HttpRequest";
import serviceApi from "@/service/api";
const listForm = ref<FormInstance>();
const props = defineProps({
  formData: String,
  formItmeList: Array,
  formId: String,
  rowId: String,
  axiosType: String,
  right_method: String,
});

const type_list = ["select", "radio", "checkBox", "tree", "switch"];
const { formData, formItmeList, formId, rowId, axiosType, right_method } = toRefs<any>(
  props
);
console.log(
  formData.value,
  formItmeList.value,
  formId.value,
  rowId.value,
  axiosType.value,
  right_method.value
);
const submit = async (formEl: FormInstance | undefined) => {
  console.log(formData.value);
  await formEl.validate((valid) => {
    if (valid) {
      console.log(formItmeList.value);
      axios
        .request({
          url:
            right_method.value == "post"
              ? serviceApi.formData_curd
              : serviceApi.formData_curd + "/" + rowId.value,
          method: right_method.value,
          data: {
            formId: formId.value,
            valueMap: formData.value,
          },
        })
        .then((res) => {
          console.log(res);
          ElMessage.success("操作成功！");
          EventBus.emit("submit_succ");
        })
        .catch((err) => {
          ElMessage.error(err);
        });
    }
  });
};
</script>

<style lang="scss" scoped></style>
